package com.commerce.reader.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.bumptech.glide.Glide;
import com.commerce.reader.Entity.Book;
import com.commerce.reader.Entity.Books;
import com.commerce.reader.Entity.Chapter;
import com.commerce.reader.R;
import com.commerce.reader.adapter.MyListAdapter;
import com.commerce.reader.adapter.MycardAdapter;
import com.commerce.reader.ui.home.HomeViewModel;
import com.google.gson.Gson;

import org.litepal.LitePal;

import java.io.Serializable;
import java.util.List;

public class DescActivity extends AppCompatActivity {


    private ImageView mAdcImgBookimg;
    private TextView mAdcTvName;
    private TextView mAdcTvTime;
    private TextView mAdcTvDesc;
    private ListView mAdcLvChapter;
    private DescViewModel descViewModel;
    private Book bookClone =null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_desc);
        descViewModel = new ViewModelProvider(this).get(DescViewModel.class);
        initView();
        final Books books = (Books) getIntent().getSerializableExtra("books");
        descViewModel.getBook(books).observe(this, new Observer<Book>() {
            @Override
            public void onChanged(Book book) {
                Glide.with(DescActivity.this).load(book.getImgurl()).into(mAdcImgBookimg);
                mAdcTvName.setText(book.getBook());
                mAdcTvTime.setText(book.getName()+"\n"+book.getTime());
                mAdcTvDesc.setText(book.getDesc());
                mAdcLvChapter.setAdapter(new MyListAdapter(DescActivity.this,book.getMap()));
            }
        });
        mAdcLvChapter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                bookClone=descViewModel.getBook(books).getValue();
                bookClone.setIds((int) l);
                LitePal.deleteAll(Chapter.class,"idName = ?",bookClone.getBook()+bookClone.getName());
                LitePal.saveAll(bookClone.getMap());
                LitePal.deleteAll(Book.class,"name = ? and book = ?",bookClone.getName(),bookClone.getBook());
                bookClone.setMap(null);
                boolean save = bookClone.save();
                List<Book> all = LitePal.findAll(Book.class);
                Log.d("TAG", "onItemClick: "+all.size()+save);
                Intent intent = new Intent(DescActivity.this, ReadActivity.class);
                intent.putExtra("ids",(int) l);
                intent.putExtra("book",bookClone.getBook());
                intent.putExtra("name",bookClone.getName());
                intent.putExtra("bookLitepal", bookClone);
                startActivity(intent);
                finish();
            }
        });
    }

    private void initView() {
        mAdcImgBookimg = findViewById(R.id.adc_img_bookimg);
        mAdcTvName = findViewById(R.id.adc_tv_name);
        mAdcTvTime = findViewById(R.id.adc_tv_time);
        mAdcTvDesc = findViewById(R.id.adc_tv_desc);
        mAdcLvChapter = findViewById(R.id.adc_lv_chapter);
    }
}